import { Injectable } from '@nestjs/common';
import { CreateAdvDto } from './dto/create-adv.dto';
import { UpdateAdvDto } from './dto/update-adv.dto';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Adv } from './entities/adv.entity';

@Injectable()
export class AdvService {
  constructor(
    @InjectRepository(Adv) 
    private advRepository: Repository<Adv>
) {}

  /**
   * 创建广告
   * @param createAdvDto 创建广告的DTO
   * @returns 广告实体
   */
  async create(createAdvDto: CreateAdvDto) {
    console.log('[ createAdvDto ]-21', createAdvDto)
    const adv = this.advRepository.create(createAdvDto);
    return this.advRepository.save(adv);
  }

  findAll() {
    return `This action returns all adv`;
  }

  findOne(id: number) {
    return `This action returns a #${id} adv`;
  }

  /**
   * 更新广告
   * @param id 广告ID
   * @param updateAdvDto 更新广告的DTO
   * @returns 广告实体
   */
  async update(id: number, updateAdvDto: UpdateAdvDto) {
    console.log('[ updateAdvDto ]-42', updateAdvDto)
    const adv = await this.advRepository.findOne({ where: { id } });
    if (!adv) {
      throw new Error('广告不存在');
    }
    Object.assign(adv, updateAdvDto);
    return this.advRepository.save(adv);
  }

  remove(id: number) {
    return `This action removes a #${id} adv`;
  }
}
